"use strict";
/* Controllers */
app.controller("dataMetricController", ['$scope', '$location', '$rootScope', '$routeParams', '$compile', 'dataMetricService', function ($scope, $location, $rootScope, $routeParams, $compile, dataMetricService) {

    var vm = $scope.vm = {};
    vm.result = {};
    vm.p = {};
    vm.p.id = $routeParams.id; //
    vm.p.bizArea = 'HRG指标';
    vm.p.url = '';
    //vm.isok = false;

    var errorFn = function (result) {
        alert(result);
    };
    $scope.listMetric = function (bizArea) {
        var vm = $scope.vm;
        // 请求后台
        vm.result.ms = "...";
        var param = {bizArea: bizArea};
        var procFn = function (response) {
            var data = response.data;
            if (data == null) {
                return;
            }
            vm.result.ms = response.ms;
            var tbl = $('#list_table');
            tbl.bootstrapTable('load', data);
            $compile(tbl)($scope);

            $scope.listUserSubscribe()

//            $("[data-subscribe]").on("click",function(){
//                var vm = $scope.vm;
//                var params = {};
//                var subscribe = $(this)
//                var id = subscribe.attr("data-subscribe")
//                params.id=id
//                var procFn = function (response) {
//                    if (response.success) {
//                        subscribe.hide()
//                        $("[data-cancelSubscribe="+id+"]").show()
//                    } else {
//                        alert(response.msg);
//                    }
//                };
//                dataMetricService.subscribe(params,procFn,errorFn)
//            })
            $("#list_table").on("click","[data-subscribe]",function(){
                var vm = $scope.vm;
                var params = {};
                var subscribe = $(this)
                var id = subscribe.attr("data-subscribe")
                params.id=id
                var procFn = function (response) {
                    if (response.success) {
                        subscribe.hide()
                        $("[data-cancelSubscribe="+id+"]").show()
                    } else {
                        alert(response.msg);
                    }
                };
                dataMetricService.subscribe(params,procFn,errorFn)
            })

            $("#list_table").on("click","[data-cancelSubscribe]",function(){
                var vm = $scope.vm;
                var params = {};
                var cancelSubscribe=$(this)
                var id = cancelSubscribe.attr("data-cancelSubscribe")
                params.id=id
                var procFn = function (response) {
                    if (response.success) {
                        cancelSubscribe.hide()
                        $("[data-subscribe="+id+"]").show()
                    } else {
                        alert(response.msg);
                    }
                };
                dataMetricService.cancelSubscribe(params,procFn,errorFn)

            })
            // 匹配节点
            //var findText = function(arr, text) {
            //    var idx = -1;
            //    for(var i=0;i<arr.length;++i) {
            //        if(arr[i].text==text) {
            //            idx = i;
            //            break;
            //        }
            //    }
            //    return idx;
            //};
            //var addNode = function(tree, biz, group, node) {
            //    var bizNode;
            //    var bizIdx = findText(tree, biz);
            //    if(bizIdx==-1) {
            //        bizNode = {text: biz, nodes:[]};
            //        tree.push(bizNode);
            //    }else {
            //        bizNode = tree[bizIdx];
            //    }
            //
            //    var groupNode;
            //    var groupIdx = findText(bizNode.nodes, group);
            //    if(groupIdx==-1) {
            //        groupNode = {text: group, nodes:[]};
            //        bizNode.nodes.push(groupNode);
            //    }else {
            //        groupNode = bizNode.nodes[groupIdx];
            //    }
            //    groupNode.nodes.push(node);
            //};
            //var treeData = function(data) {
            //    var currArea,currGroup,currTopic;
            //    var arr = [];
            //    var item = null;
            //    for(var i=0; i<data.length; ++i) {
            //        if(currArea!=data[i].bizArea || currGroup!=data[i].topicGroup || currTopic!=data[i].topic) {
            //            currArea=data[i].bizArea;
            //            currGroup=data[i].topicGroup;
            //            currTopic = data[i].topic;
            //
            //            item = {area: data[i].bizArea, group:data[i].topicGroup, topic: data[i].topic, items: []};
            //            arr.push(item);
            //        }
            //        item.items.push(data[i]);
            //    }
            //    // 针对聚合后的数据进行聚合
            //    var tree =[];
            //    for(var n=0; n<arr.length; ++n) {
            //        var node = arr[n];
            //        var leaf = {text: node.topic, href: JSON.stringify(node.items)};
            //        addNode(tree, node.area, node.group, leaf);
            //    }
            //    return tree;
            //};

            //vm.isok = true;
            // 将数据显示到tree中
            //$('#tree').treeview({
            //    data: treeData(data),
            //    onNodeSelected: function (event, data) {
            //        if(data.href==null) return;
            //        $('#list_table').bootstrapTable('load', JSON.parse(data.href));
            //        //var element = angular.element($("#info_detail"));
            //        //var scope = element.scope();
            //        //scope.$apply(function () {
            //        //    scope.vm.p.url = data.href;
            //        //});
            //    }
            //});
        };
        dataMetricService.listMetric(param, procFn, errorFn);
    };

    $scope.initializeController = function () {
        vm.result.ms = '';
        var $resultTable = $('#list_table');
        $resultTable.bootstrapTable({
             pageList: [100, 300, 500, 1000, 5000], pageSize: 500
        });
        $resultTable.bootstrapTable('hideColumn', 'indexCn');
        $resultTable.bootstrapTable('hideColumn', 'indexEn');
        $scope.listMetric(vm.p.bizArea);
    };

    $scope.initializeController();


    $scope.listUserSubscribe = function () {
        var params={}
        var procFn = function (response) {
            if (response.success) {
                var res=response.data
                for(var index in response.data){
                    var metricId=res[index].metricId
                    $("[data-subscribe="+metricId+"]").hide()
                    $("[data-cancelSubscribe="+metricId+"]").show()
                }
            } else {
                alert(response.msg);
            }
        };
        dataMetricService.listUserSubscribe(params,procFn,errorFn)
//        return dataMetricService.subscribe(params, procFn, errorFn);
    };




}]);


/* Services */
app.service('dataMetricService', ['ajaxService', function (ajaxService) {
    this.listMetric = function (params, successFunction, errorFunction) {
        ajaxService.AjaxPost(params, "/star/api/basic-service/v1/metric_define/listMetric.do", successFunction, errorFunction);
    };

    this.subscribe = function (params, successFunction, errorFunction) {
        ajaxService.AjaxPost(params, "/star/api/basic-service/v1/metric_define/subscribe.do", successFunction, errorFunction);
    };

    this.cancelSubscribe = function (params, successFunction, errorFunction) {
        ajaxService.AjaxPost(params, "/star/api/basic-service/v1/metric_define/cancelSubscribe.do", successFunction, errorFunction);
    };

    this.listUserSubscribe = function (params, successFunction, errorFunction) {
        ajaxService.AjaxPost(params, "/star/api/basic-service/v1/metric_define/listUserSubscribe.do", successFunction, errorFunction);
    };
}]);




